package gogoqiu.java.base.http.api;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.gson.reflect.TypeToken;

import gogoqiu.java.base.console;
import gogoqiu.java.base.http.response.BaseJsonResponse;
import gogoqiu.java.base.http.response.StandardResponse;
import gogoqiu.java.base.http.utils.BaseRespBodyProcess;
import gogoqiu.java.base.http.utils.BaseStdRespListProcess;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class simple {
    
    public static abstract class StdRespListProcess
            extends BaseRespBodyProcess<List<StandardResponse<String>>> {
        public StdRespListProcess(){  
            super();
        }
    }
    public static void fetch( ) {
        try{
            OkHttpClient client = new OkHttpClient.Builder()
                .build();
            StdRespListProcess process = new StdRespListProcess() {

                @Override
                public void OnHttpRespBodyEmpty(int code) {
                    console.outputErr( "OnHttpRespBodyEmpty: code={}", code );
                }

                @Override
                public void OnGetContentError(int code) {
                    console.outputErr( "OnGetContentError: code={}", code );
                }

                @Override
                public void OnGsonError(String url, int code, long contentLength, String content) {
                    console.outputErr( "OnGsonError: url={}, code={}, contentLength={}, content={}", url, code, contentLength, content );
                }

                /*
                 * [ { "data": [], "retVal": false, "code": 0, "msg": "未知标签: drafts|set|get|setfile" } ]
                 * 可以理解为class com.google.gson.internal.LinkedTreeMap吗
                 * Gson 的默认解析规则会将：
                    JSON 对象（{}）解析为 LinkedTreeMap（实现 Map 接口，保存键值对）；
                    JSON 数组（[]）解析为 ArrayList（实现 List 接口，保存元素）。
                 */
                @Override
                public void OnResps(List<StandardResponse<String>> resps) {
                    int idx = resps.size() - 1;
                    if( resps.size()>0 && resps.get( idx ).getRetVal()) {
                        
                    }else{
                        
                    }
                    console.outputInfo("OnResps: resps={}", resps);
                }

                @Override
                public Type getGsonParseType() {
                    return new TypeToken<List<StandardResponse<String>>>(){}.getType();
                }   
            };
            FormBody.Builder formBodyBuilder = new FormBody.Builder();

            formBodyBuilder.add("cmd", "response");
            formBodyBuilder.add("content", "content" );
            formBodyBuilder.add("source", "0"); //Source.android
            formBodyBuilder.add("editor", "1" );//editor.fileUpload

            Request request = new Request.Builder()
                    .post(formBodyBuilder.build())
                    .url( "https://mydear.sinaapp.com/usr/bin/vscode_options.php" )
                    .build();
            //okhttp 同步请求关键代码
            Response response = client.newCall(request).execute();
            process.parse( "https://mydear.sinaapp.com/usr/bin/vscode_options.php", response );
            //List<BaseJsonResponse> result =  check.getResult( url, response );            
        }catch( IOException e ){
            console.outputErr( "fetch", e );
        }        
    }

    //private static final Logger logger = LogManager.getLogger(simple.class);

    public static void main(String[] args) {
        fetch();
    }
}
